package com.thm.tfast.modules.sys.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.thm.tfast.core.entity.ViewResult;
import com.thm.tfast.modules.sys.model.SysResource;
import com.thm.tfast.modules.sys.model.SysResourceCategory;
import com.thm.tfast.modules.sys.model.dto.resource.ResourceCategoryInsertDto;
import com.thm.tfast.modules.sys.model.dto.resource.ResourceCategoryUpdateDto;
import com.thm.tfast.modules.sys.model.dto.resource.ResourceInsertDto;
import com.thm.tfast.modules.sys.model.dto.resource.ResourceUpdateDto;
import com.thm.tfast.modules.sys.service.SysResourceCategoryService;
import com.thm.tfast.modules.sys.service.SysResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 资源信息 前端控制器
 * </p>
 *
 * @author THM
 * @since 2022-10-10
 */
@RestController
@Api(tags = "系统：资源管理")
@RequestMapping("/sysResource")
public class SysResourceController {

    @Resource
    private SysResourceService sysResourceService;
    @Resource
    private SysResourceCategoryService sysResourceCategoryService;

    @ApiOperation("根据条件分页获取资源列表")
    @GetMapping(value = "/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "关键字：资源名称、资源路径"),
            @ApiImplicitParam(name = "categoryId", value = "资源类型ID"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true)
    })
    public ViewResult<Page<SysResource>> page(@RequestParam(value = "keyword", required = false) String keyword,
                                              @RequestParam(value = "categoryId", required = false) Long categoryId,
                                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SysResource> page = sysResourceService.page(keyword, categoryId,pageNum, pageSize);
        return ViewResult.success(page);
    }

    @ApiOperation("获取指定资源信息")
    @GetMapping("/{resourceId}")
    public ViewResult<SysResource> getResourceById(@PathVariable Long resourceId) {
        SysResource sysResource = sysResourceService.getById(resourceId);
        return ViewResult.success(sysResource);
    }

    @ApiOperation("新增资源信息")
    @PostMapping("/insert")
    public ViewResult<Long> insert(@Validated @RequestBody ResourceInsertDto dto){
        return ViewResult.success(sysResourceService.insert(dto));
    }

    @ApiOperation("修改资源信息")
    @PutMapping("/update")
    public ViewResult<String> update(@Validated @RequestBody ResourceUpdateDto dto) {
        boolean success = sysResourceService.update(dto);
        if (success) {
            return ViewResult.success();
        }
        return ViewResult.error();
    }

    @ApiOperation("删除资源信息")
    @DeleteMapping("/delete/{resourceId}")
    public ViewResult<String> delete(@PathVariable Long resourceId){
        boolean success = sysResourceService.delete(resourceId);
        if (success) {
            return ViewResult.success();
        }
        return ViewResult.error();
    }

    @ApiOperation("根据条件分页获取资源类型列表")
    @GetMapping(value = "/category/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "关键字：资源类型名称"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true)
    })
    public ViewResult<Page<SysResourceCategory>> resourceCategoryPage(@RequestParam(value = "keyword", required = false) String keyword,
                                                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        Page<SysResourceCategory> page = sysResourceCategoryService.page(keyword, pageNum, pageSize);
        return ViewResult.success(page);
    }

    @ApiOperation("获取指定资源类型信息")
    @GetMapping("/category/{categoryId}")
    public ViewResult<SysResourceCategory> getResourceCategoryById(@PathVariable Long categoryId) {
        SysResourceCategory sysResourceCategory = sysResourceCategoryService.getById(categoryId);
        return ViewResult.success(sysResourceCategory);
    }

    @ApiOperation("新增资源类型信息")
    @PostMapping("/category/insert")
    public ViewResult<Long> insertCategory(@Validated @RequestBody ResourceCategoryInsertDto dto){
        return ViewResult.success(sysResourceCategoryService.insert(dto));
    }

    @ApiOperation("修改资源类型信息")
    @PutMapping("/category/update")
    public ViewResult<String> updateCategory(@Validated @RequestBody ResourceCategoryUpdateDto dto) {
        boolean success = sysResourceCategoryService.update(dto);
        if (success) {
            return ViewResult.success();
        }
        return ViewResult.error();
    }

    @ApiOperation("删除资源类型信息")
    @DeleteMapping("/category/delete/{categoryId}")
    public ViewResult<String> deleteCategory(@PathVariable Long categoryId){
        boolean success = sysResourceCategoryService.delete(categoryId);
        if (success) {
            return ViewResult.success();
        }
        return ViewResult.error();
    }
}

